/**
 * Created by jinhongglin on 15-11-27.
 */
app.controller("total_buy_information_controller",function($scope,$location) {
    $scope.go_to_home = function () {
        $location.path("/")
    }
    judge_contain_meal($scope)

    console.log(document.getElementsByTagName('price_element').color)

})
function judge_contain_meal($scope){
    if (localStorage.getItem("total_informations") == null) {
        $scope.not_set_people = ['赵大', '钱二', '张三', '李四', '王五', '赵六'];
        $scope.all_people = $scope.not_set_people.length;
        $scope.set_number = 0 ;
        $scope.final_price = 0;
    }
    else
    {
        //finall_set_meal_information = JSON.parse(localStorage.getItem("total_informations"))
        $scope.all_person_name = ['赵大', '钱二', '张三', '李四', '王五', '赵六'];

        var set_person_name = get_set_name(JSON.parse(localStorage.getItem("total_informations")));
        $scope.set_number = order_people(set_person_name).length;
        var price = get_set_price(JSON.parse(localStorage.getItem("total_informations")));
        var all_price = get_all_price(price)
        var final_price = get_final_price(all_price)
        $scope.final_price = (final_price).toFixed(2);
        var all_person_name = $scope.all_person_name;
        $scope.not_set_people = get_not_set_person_name(all_person_name, set_person_name);
        $scope.all_people = $scope.not_set_people.length;
    }
}
function order_people(set_person_name){
    var uniquel_name = [];
    var temple ={};
    for(var i=0;i<set_person_name.length;i++){
        if(temple[set_person_name[i]] == undefined){

            uniquel_name.push(set_person_name[i])
            temple[set_person_name[i]] = 1;
        }
    }
    return uniquel_name;
}
function get_set_name (total_informatioin) {
    var set_person_name=[]
    for (i = 0; i < total_informatioin.length; i++) {
        set_person_name.push(total_informatioin[i].set_meal_person_name);
    }
    return set_person_name;
}
function get_set_price(total_informatioin){
    var price = [];
    for (i = 0; i < total_informatioin.length; i++) {
        price.push(total_informatioin[i].set_meal_price)

    }
    return price;
}
function get_all_price(price)
{
    var all_price =[];
    for(i = 0 ;i<price.length;i++)
    {
        //console.log(price[i].substr(1))
        all_price.push(parseFloat(price[i].substr(1)))
    }
    return all_price;
}
function get_final_price(all_price){
    var final_price=0;
    for(i=0;i<all_price.length;i++) {
        final_price += all_price[i]
    }
    return final_price;
}
function get_not_set_person_name(all_person_name,set_person_name){

    for(i=0;i<all_person_name.length;i++){
        for(j=0;j<set_person_name.length;j++){
            if(all_person_name[i] != set_person_name[j] )
            {

                continue;
            }
            else if(all_person_name[i] == set_person_name[j]){
                all_person_name.splice(i,1)
            }
        }
    }
    return all_person_name;
}

